package com.tb.dubbo_service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tb.dubbo.IUserService;
import com.tb.mapper.UserMapper;
import com.tb.pojo.User;
import com.tb.pojo.UserExample;
import com.tb.utils.IdUtils;
import com.tb.utils.MD5;
import com.tb.utils.ReturnList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    //普通用户登录验证
    @Override
    public User checkOne(User user) {

        return userMapper.selectByUser(user);
    }

    //后台管理登录验证
    @Override
    public User checkAdmin(User user) {
        return userMapper.selectAdminByUser(user);
    }

    //增加管理员用户
    @Override
    public boolean addAdmin(User user) {

        //验证是否已存在
        User admin = userMapper.selectAdminByUser(user);

        if (admin==null){
            //新增账号
            String id=IdUtils.genImageName();
            user.setUserId(id);
            String pass=user.getUserPass();
            MD5 md5=new MD5();
            user.setUserPass( md5.getMD5ofStr(pass));
            int i = userMapper.insertSelective(user);
            return i >= 1;
        }
        return false;
    }


    //修改管理员账号
    @Override
    public boolean updateAdmin(User user) {
        String pass=user.getUserPass();
        if(!(pass==null|| "".equals( pass ))){
            MD5 md5=new MD5();
            user.setUserPass( md5.getMD5ofStr(pass));
        }
        int i=userMapper.updateByPrimaryKeySelective(user);
        return i >= 1;
    }

    @Override
    public boolean getEmail(String email) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        //查询此邮箱的用户
        criteria.andUserEmailEqualTo(email);

        List<User> users = userMapper.selectByExample( example );
        return users.size() < 1;
    }

    @Override
    public boolean getUserName(String userName) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        //查询此用户名的数据
        criteria.andUserNameEqualTo(userName);

        List<User> users = userMapper.selectByExample( example );
        return users.size() < 1;
    }

    @Override
    public boolean addUser(User user) {
        int insert = userMapper.insertSelective( user );
        return insert>=1;
    }

    @Override
    public List<User> selectById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public ReturnList getUsers(Integer page, Integer rows,Integer roler) {
        PageHelper.startPage(page, rows);

        UserExample example= new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        /*查询管理员列表*/
        if (roler==0) {
            List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
            criteria.andRolerIdIn(list);
        }else {
            /*查询普通用户列表*/
            criteria.andRolerIdEqualTo(3);
        }

        List<User> users = userMapper.selectByExample( example );

        PageInfo<User> pageInfo = new PageInfo<>(users);

        return ReturnList.get( pageInfo.getTotal(), pageInfo.getList() );
    }

    @Override
    public int updateAddress(Map<String, Object> map) {
        int flag = userMapper.updateAddress(map);
        return flag;
    }

    @Override
    public User selectAddress(String userId) {
        User user = userMapper.selectAddress(userId);
        System.out.println(user.getUserAddressArea());
        return user;
    }
}
